export class UIResult_images {
    protected _panel: gui.Widget;
    private _srcH: number;
    protected data: any;


    protected _create(columns:number, onselect:(id:number)=>void) {
        if (this._panel) return;
        new gui.Box()
        let path = 'editorResources/AIUI/propertyItem/' + name + ".widget";
        this._panel = gui.UIPackage.createWidgetSync(path);
        this._srcH = this._panel.height;
        this._panel.name = o.name;
        this.o = o;
        this.data = data;
        let title = this._panel.getChild("title");
        if (title) {
            title.text = o.label ?? o.name;
        }
        if (null != def) {
            if (null != o.default) {
                def = o.default;
            }
            if (null != data[o.name]) {
                def = data[o.name];
            } else {
                data[o.name] = def;
            }
            this._setValue(def);

            let val = this._panel.getChild("value");
            if (val) {
                val.on("changed", this._changeHandle, this);
                if (null != o.min) {
                    (val as any).min = o.min;
                }
                if (null != o.max) {
                    (val as any).max = o.max;
                }
                if (null != o.step) {
                    (val as any).step = o.step;
                }
            }
        }
        if (data) {
            IEditor.DataWatcher.addListener(data, this._watchHandle, this);
        }
        if (null != o.hidden) {
            if (true === o.hidden) {
                this._panel.visible = false;
                this._panel.height = 0.01;
            } else if ('string' == typeof o.hidden) {
                this._watchHandle();
            }
        }
        return this._panel;
    }
    protected _changeHandle(e: gui.Event) {
        let txt = (e.target as any).text;
        this.data[this.o.name] = txt;
        this._checkChange();
    }
    protected _checkChange() {
        if (this.o.onChange) {
            this.o.onChange(this.o.name, this.data[this.o.name], this.data);
        }
    }

    protected _watchHandle(sender?: any, target?: any, key?: string, value?: any, oldvalue?: any, extInfo?: any) {
        let data = this.data;
        if (this.o.hidden) {
            let val = eval(this.o.hidden as string);
            if (val) {
                this._panel.visible = false;
                this._panel.height = 0.01;
            } else {
                this._panel.visible = true;
                this._panel.height = this._srcH;
            }
        }
        this._setValue(data[this.o.name]);
    }



    protected _setValue(val: any) {
        let tf = this._panel.getChild("value");
        if (tf)
            tf.text = String(val);
    }
}